<template>
  <el-form ref="formRef" :model="formData" :rules="formRules" label-width="120px">
    <el-row>
      <el-col :span="12">
        <el-form-item label="表名称" prop="tableName">
          <el-input v-model.trim="formData.tableName" placeholder="请输入表名称" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="表描述" prop="tableComment">
          <el-input v-model="formData.tableComment" placeholder="请输入表描述" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item prop="className">
          <template #label>
            <span>
              实体类名称
              <el-tooltip
                content="默认去除表名的前缀。如果存在重复，则需要手动添加前缀，避免 MyBatis 报 Alias 重复的问题。"
                placement="top"
              >
                <Icon icon="ep:question-filled" />
              </el-tooltip>
            </span>
          </template>
          <el-input v-model.trim="formData.className" placeholder="请输入实体类名称" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="作者" prop="author">
          <el-input v-model="formData.author" placeholder="请输入作者" />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" type="textarea" :rows="3" placeholder="请输入备注" />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="ts">
import * as CodegenApi from '@/api/infra/codegen'
import { PropType } from 'vue'
import { FormRules } from 'element-plus'

defineOptions({ name: 'InfraCodegenBasicInfoForm' })

/*
  defineProps 定义的 props 默认是非响应性的，即直接访问 props.table 的时候，它不是响应式的。但当 props 被传递到模板或组合式 API（如 watch、computed 等）中时，
  Vue 会自动进行代理并提供响应式行为。
  在模板中使用:
    <div>{{ table.name }}</div>，Vue 会为 props.table 创建代理，确保它在模板中具有响应性。
  在组合式 API 中使用:
    watch(
      () => props.table,
      (newValue, oldValue) => {
        console.log("Table updated:", newValue);
      }
    )
 */
const props = defineProps({
  table: Object as PropType<Nullable<CodegenApi.CodegenTableVO>>
  // default: () => null
})

const formData = ref({
  tableName: '',
  tableComment: '',
  className: '',
  author: '',
  remark: ''
})
const formRules = reactive<FormRules>({
  tableName: [required],
  tableComment: [required],
  className: [required],
  author: [required]
})
const formRef = ref()

// 外层传入props.table的对象变更也会监听并触发formData.value同步变更(保持与外层为同一对象)
watch(
  () => props.table,
  (table) => {
    if (!table) return
    formData.value = table
  },
  {
    immediate: true,
    deep: true
  }
)

defineExpose({
  validate: async () => await formValidate(formRef)
})
</script>

<style scoped lang="scss"></style>
